/**
 * 
 */
package com.omega.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.omega.constants.ApplicationConstants;
import com.omega.dao.SupplierDao;
import com.omega.entity.Supplier;
import com.omega.service.SupplierService;

/**
 * @author vpan14
 * 
 */
@Service
public class SupplierServiceImpl implements SupplierService {

	@Autowired
	SupplierDao supplierDao;

	/**
	 * 
	 */
	public SupplierServiceImpl() {
		// TODO Auto-generated constructor stub
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void addSupplier(Supplier supplier) {
		supplier.setSupplierActive(ApplicationConstants.SUPPLIER_ACTIVE);
		supplierDao.addSupplier(supplier);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public List<Supplier> loadAllSuppliers() {
		return supplierDao.loadAllSuppliers();
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public Supplier loadById(Integer supplierId) {
		return supplierDao.loadById(supplierId);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateSupplier(Supplier supplier) {
		Supplier supplierEntity = supplierDao.loadById(supplier.getSupplierId());
		supplierEntity.setTinNumber(supplier.getTinNumber());
		supplierEntity.setSupplierName(supplier.getSupplierName());
		supplierEntity.setSupplierAddress(supplier.getSupplierAddress());
		supplierEntity.setSupplierEmailId(supplier.getSupplierEmailId());
		supplierEntity
				.setSupplierPhoneNumber(supplier.getSupplierPhoneNumber());
		supplierEntity.setSupplierMobileNumber(supplier
				.getSupplierMobileNumber());
		supplierEntity.setMonth(supplier.getMonth());
		supplierEntity.setYear(supplier.getYear());
		supplierDao.updateSupplier(supplierEntity);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void activateSupplier(long supplierId) {
		Supplier supplierEntity = supplierDao.loadById(supplierId);
		supplierEntity.setSupplierActive(ApplicationConstants.SUPPLIER_ACTIVE);
		supplierDao.updateSupplier(supplierEntity);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void deactivateSupplier(long supplierId) {
		Supplier supplierEntity = supplierDao.loadById(supplierId);
		supplierEntity
				.setSupplierActive(ApplicationConstants.SUPPLIER_INACTIVE);
		supplierDao.updateSupplier(supplierEntity);
	}

}
